package com.heima.createMethods;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: Ethan Yankang
 * @Program: 面试必会代码
 * @Date: 2024-08-28 18:06
 **/
public class ImplCallable implements Callable {
    @Override
    public String call() throws Exception {
        System.out.println("我和前面的Runnable接口只是有无返回值的区别如下哦：~");
        System.out.println("1.我有返回值，而Runnable没有哦");
        System.out.println("2.我可以抛出异常，而Runnable不可以哦");
        System.out.println("3.我在传入线程类执行之前需要先传入FutureTask类的构造方法来实现异步调用哦");

        return "\n线程创建方式三：\n" +
                "通过向FuturnTasK类构建函数传入实现了Callable接口的类的实例后，再将FutureTask类的实例传入Thread的构建函数中，在调用启动线程即可";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ImplCallable byImplCallable = new ImplCallable();
        FutureTask<String> futureTask = new FutureTask<String>(byImplCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());
    }
}
